<template>
	<view class="">
		<view class="content">
			<view class="logo">
				<view class="img"><u-image width="200rpx" height="200rpx" mode="widthFix" :src="src"></u-image></view>
				<view class="title"><text>悦彩配送</text></view>
			</view>
			<u-line color="#666666" />
			<view class="info">
				<view class="info-title">登录后应用将获得以下权限</view>
				<view class="info-tag">获得您的公开信息（昵称、头像等）</view>
			</view>
			<u-gap height="40" bg-color="#ffffff"></u-gap>
			<view class="success" v-if="cache_key == null"><u-button type="success" @click="authorize()" open-type="getUserInfo">获取微信授权</u-button></view>
			<view class="success" v-else><u-button type="warning" @click="getUserInfo()" open-type="getUserInfo">点击绑定用户信息</u-button></view>
			<view class="success"><u-button type="error" @click="goIndex()">暂不授权</u-button></view>
		</view>
		<view><u-toast ref="uToast" /></view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			src: '/static/app_logo.png',
			code2SessionStatus: false,
			wxCode: '',
			openid: '',
			cache_key:null,
		};
	},
	onLoad(op) {
		uni.removeStorageSync('encrypted_data');
		uni.removeStorageSync('iv');
		uni.removeStorageSync('wx_code');
		//uni.removeStorageSync('cache_key');
		//console.log('cache_key:' + this.cache_key);
	},
	methods: {
		authorize() {
			let _t = this;
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					console.log('weixin login' + JSON.stringify(loginRes));
					const wxCode = loginRes.code;
					console.log('code:' + wxCode);
					uni.setStorageSync('wx_code', wxCode);
					//获取用户oppenid
					_t.$u.api.xcxLogin({ code: wxCode }).then(res => {
						console.log('res');
						console.log(res);
						//新微信用户
						if (res.data.cache_key) {
							//console.log('cache_key====');
							//console.log(res.data.cache_key);
							//_t.cache_key = res.data.cache_key;
							//uni.setStorageSync('cache_key', res.data.cache_key);
							_t.getUserInfo()
						} else {
							uni.setStorageSync('token', res.data.token);
							uni.setStorageSync('userInfo', res.data.user.user);
							uni.switchTab({
								url: '/pages/center/index'
							});
						}
					});
					//
				}
			});
		},
		getUserInfo (){
			// 获取用户信息
			uni.getUserInfo({
				provider: 'weixin',
				success: function(infoRes) {
					console.log('successRes====');
					console.log('infoRes====');
					console.log(infoRes);
					uni.setStorageSync('encrypted_data', infoRes.encryptedData);
					uni.setStorageSync('iv', infoRes.iv);
					//跳转用户绑定
					uni.reLaunch({
						url: '/pages/login/binding'
					});
				},
				error:function(errorRes){
					console.log('errorRes====');
					console.log(errorRes);
				}
			});
		},
		goIndex(){
			uni.reLaunch({
				url: '/pages/index/index'
			});
		}
	}
};
</script>

<style lang="scss">
page {
	background-color: #ffffff;
	height: 100vh;
}
.content {
	margin: 0 72rpx;
	.logo {
		height: 40vh;
		padding-top: 80rpx;
		text-align: center;
		.img {
			display: flex;
			justify-content: center;
			align-items: center;
		}
		.title {
			font-size: 32rpx;
			padding-top: 24rpx;
			font-weight: bold;
		}
	}
	.info {
		.info-title {
			font-size: 30rpx;
			padding-top: 24rpx;
			font-weight: bold;
		}
		.info-tag {
			font-size: 24rpx;
			color: #82848a;
			padding-top: 8rpx;
		}
	}
	.success {
		padding-top: 40rpx;
	}
}
</style>
